//数据库函数
const path = require('path');
const {	Sequelize, DataTypes } = require("sequelize");

const sequelize = new Sequelize({
	dialect: 'sqlite',
	storage: path.resolve('shuJu/sytht.db'),
	define: {
		freezeTableName: true,
		timestamps: false
	},
	logging: false,
});
//定义模型
const sheZhi = sequelize.define("sheZhi", {
	biaoTi: {
		type: DataTypes.TEXT,
		primaryKey: true
	},
});

const yongHu = sequelize.define("yongHu", {
	mingZi: {
		type: DataTypes.TEXT,
		primaryKey: true
	},
	miMa: {
		type: DataTypes.TEXT
	},
});

const lanMu = sequelize.define("lanMu", {
	lHao: {
		type: DataTypes.INTEGER,
		primaryKey: true
	},
	fHao: {
		type: DataTypes.INTEGER,
		allowNull: false
	},
	muBan: {
		type: DataTypes.TEXT,
		allowNull: false
	},
	biaoTi: {
		type: DataTypes.TEXT
	},
	neiRong: {
		type: DataTypes.TEXT
	},
});

const wenZhang = sequelize.define("wenZhang", {
	wHao: {
		type: DataTypes.INTEGER,
		primaryKey: true
	},
	lHao: {
		type: DataTypes.INTEGER
	},
	biaoTi: {
		type: DataTypes.TEXT
	},
	neiRong: {
		type: DataTypes.TEXT
	},
	muBan: {
		type: DataTypes.TEXT
	},
	riQi: {
		type: DataTypes.TEXT
	},
});

async function sequelizeSync() {
	await sequelize.sync();
};
 
//查设置
async function chaSheZhi() {
	let a = [];
	await sequelizeSync();
	await sheZhi.findAll().then(x => {
		a = x.map(x => {
			return x.dataValues;
		})
	});
	return a;
}

//改设置
async function gaiSheZhi(dui) {
	let a;
	await sequelizeSync();
	await sheZhi.update(dui, { where: {} }).then(x => {
		a = x
	});
	return a;
}

//查栏目
async function chaLanMu() {
	let a = [];
	await sequelizeSync();
	await lanMu.findAll().then(x => {
		a = x.map(x => {
			return x.dataValues;
		})
	});
	return a;
}

async function chaLanMuDan(lHao) {
	let a = [];
	await sequelizeSync();
	await lanMu.findAll({ where:{ lHao: lHao } }).then(x => {
		a = x.map(x => {
			return x.dataValues;
		})
	});
	return a;
}

async function chaLanMuDuo(fHao, shu = 5) {
	let a = [];
	await sequelizeSync();
	await lanMu.findAll({
		where:{ fHao: fHao },
		offset: 0,
		limit: shu,
	}).then(x => {
		a = x.map(x => {
			return x.dataValues;
		})
	});
	return a;
}

//增栏目
async function zengLanMu(dui) {
	let a;
	await sequelizeSync();
	await lanMu.create(dui).then(x => {
		a = x.dataValues;
	})
	return a;
}

//改栏目
async function gaiLanMu(lHao, dui) {
	let a;
	await sequelizeSync();
	await lanMu.update(dui, { where: {lHao} }).then(x => {
		a = x;
	})
	return a;
}

//删栏目、内容
async function shanChu(hao, b) {
	let biao,h,a;
	if (b == 'lanMu') {
		h = 'lHao';
		biao = lanMu;
	}
	if (b == 'wenZhang') {
		h = 'wHao';
		biao = wenZhang;
	}
	await sequelizeSync();
	await biao.destroy({ where: { [h]: hao } }).then(x => {
		a = x;
	})
	return a;
}

//查文章
async function chaWenZhang(lHao) {
	let a = [];
	await sequelizeSync();
	if (lHao) {
		await wenZhang.findAll({ where: { lHao } }).then(x => {
			a = x.map(x => {
				return x.dataValues;
			})
		});
	}else {
		await wenZhang.findAll().then(x => {
			a = x.map(x => {
				return x.dataValues;
			})
		});
	}
	return a;
}

async function chaWenZhangDan(wHao) {
	let a = [];
	await sequelizeSync();
	await wenZhang.findAll({ where: { wHao } }).then(x => {
		a = x.map(x => {
			return x.dataValues;
		})
	});
	return a;
}

async function chaWenZhangDuo(lHao, shu = 5) {
	let a = [];
	await sequelizeSync();
	await wenZhang.findAll({ where:{ lHao }, offset: 0, limit: shu })
	.then(x => {
		a = x.map(x => {
			return x.dataValues;
		})
	});
	return a;
}

//增文章
async function zengWenZhang(dui) {
	let a;
	await sequelizeSync();
	await wenZhang.create(dui).then(x => {
		a = x.dataValues;
	})
	return a;
}

//改文章
async function gaiWenZhang(wHao, dui) {
	let a;
	await sequelizeSync();
	await wenZhang.update(dui, { where: { wHao } }).then(x => {
		a = x;
	})
	return a;
}

//查用户
async function chaYongHu() {
	let a = [];
	await sequelizeSync();
	await yongHu.findAll().then(x => {
		a = x.map(x => {
			return x.dataValues;
		})
	});
	return a;
}

//改用户
async function gaiYongHu(dui) {
	let a;
	await sequelizeSync();
	await yongHu.update(dui, { where: {} }).then(x => {
		a = x
	});
	return a;
}

let shuJuKu = {
	chaLanMuDuo,
	chaLanMu,
	chaLanMuDan,
	zengLanMu,
	gaiLanMu,
	chaWenZhang,
	chaWenZhangDuo,
	chaWenZhangDan,
	zengWenZhang,
	gaiWenZhang,
	shanChu,
	chaSheZhi,
	gaiSheZhi,
	chaYongHu,
	gaiYongHu
}
module.exports = shuJuKu;
